﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.圣同润.merchant;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 广州爱特安为科技.UC.圣同润.客服
{
    public partial class Form物流信息 : DevExpress.XtraEditors.XtraForm
    {
        //  在途
        private String brandNameTransit;
        private int brandIdTransit;
        //  签收
        private String brandNameAccepted;
        private int brandIdAccepted;
        //  退回和退签
        private String brandNameReturn;
        private int brandIdReturn;

        public Form物流信息()
        {
            InitializeComponent();
            //  待揽收布局
            this.gridView待揽收订单.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "order_sn");
            this.gridView待揽收订单.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "total_num");
            this.gridView待揽收订单.GroupFormat = "{1} {2}";  //默认"{0}: [#image]{1} {2}"; 字段名称：数据 计数=？
            this.gridView待揽收订单.GroupSummary[gridView待揽收订单.GroupSummary.Count - 1].DisplayFormat = "货品数量合计：{0:N0}";

            //  在途
            this.searchLookUpEdit在途品牌商.Properties.DataSource = MerchantService.getBrandAll();
            //  在途布局
            this.gridView在途订单.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "order_sn");
            this.gridView在途订单.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "total_num");
            this.gridView在途订单.GroupFormat = "{1} {2}";  //默认"{0}: [#image]{1} {2}"; 字段名称：数据 计数=？
            this.gridView在途订单.GroupSummary[gridView在途订单.GroupSummary.Count - 1].DisplayFormat = "货品数量合计：{0:N0}";

            //  签收
            this.searchLookUpEdit签收品牌商.Properties.DataSource = MerchantService.getBrandAll();
            //  签收布局
            this.gridView签收订单.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "order_sn");
            this.gridView签收订单.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "total_num");
            this.gridView签收订单.GroupFormat = "{1} {2}";  //默认"{0}: [#image]{1} {2}"; 字段名称：数据 计数=？
            this.gridView签收订单.GroupSummary[gridView签收订单.GroupSummary.Count - 1].DisplayFormat = "货品数量合计：{0:N0}";

            //  退回和退签
            this.searchLookUpEdit退回和退签品牌商.Properties.DataSource = MerchantService.getBrandAll();
            //  退回和退签布局
            this.gridView退回和退签订单.GroupSummary.Add(DevExpress.Data.SummaryItemType.Count, "order_sn");
            this.gridView退回和退签订单.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "total_num");
            this.gridView退回和退签订单.GroupFormat = "{1} {2}";  //默认"{0}: [#image]{1} {2}"; 字段名称：数据 计数=？
            this.gridView退回和退签订单.GroupSummary[gridView退回和退签订单.GroupSummary.Count - 1].DisplayFormat = "货品数量合计：{0:N0}";
        }

        #region 待揽收

        private void simpleButton待揽收数据刷新_Click(object sender, EventArgs e)
        {
            this.getDeliveryNotAcceptedData();
        }
        private void getDeliveryNotAcceptedData()
        {
            this.gridControl待揽收订单.DataSource = MerchantService.getDeliveryNotAcceptedOrderLogisticsInfo();
            this.gridView待揽收订单.RefreshData();
            this.gridView待揽收订单.BestFitColumns();
        }
        
        #endregion

        #region 在途
        private void searchLookUpEdit在途品牌商_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear在途品牌商Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit在途品牌商View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit在途品牌商.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String id = this.searchLookUpEdit在途品牌商.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.brandIdTransit = Convert.ToInt32(id);
                    this.brandNameTransit = this.searchLookUpEdit在途品牌商.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    //  下拉选品牌商，查询在途信息
                    this.gridControl在途订单.DataSource = MerchantService.getDeliveryOrderTransitInfo(this.brandIdTransit);
                    this.gridView在途订单.RefreshData();
                    gridView在途订单.OptionsBehavior.AutoExpandAllGroups = true;
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear在途品牌商Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit在途品牌商.ToolTip = null;
            searchLookUpEdit在途品牌商.EditValue = null;
        }
        private void searchLookUpEdit在途品牌商_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.brandNameTransit;
        }
        #endregion

        #region 签收
        private void searchLookUpEdit签收品牌商_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear签收品牌商Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit签收品牌商View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit签收品牌商.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String id = this.searchLookUpEdit签收品牌商.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.brandIdAccepted = Convert.ToInt32(id);
                    this.brandNameAccepted = this.searchLookUpEdit签收品牌商.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    //  下拉选品牌商，查询在途信息
                    this.gridControl签收订单.DataSource = MerchantService.getDeliveryOrder3(this.brandIdAccepted);
                    this.gridView签收订单.RefreshData();
                    gridView签收订单.OptionsBehavior.AutoExpandAllGroups = true;
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear签收品牌商Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit签收品牌商.ToolTip = null;
            searchLookUpEdit签收品牌商.EditValue = null;
        }
        private void searchLookUpEdit签收品牌商_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.brandNameAccepted;
        }
        #endregion

        #region 退回和退签
        private void searchLookUpEdit退回和退签品牌商_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear退回和退签品牌商Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit退回和退签品牌商View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit退回和退签品牌商.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String id = this.searchLookUpEdit退回和退签品牌商.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.brandIdReturn = Convert.ToInt32(id);
                    this.brandNameReturn = this.searchLookUpEdit退回和退签品牌商.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    //  下拉选品牌商，查询在途信息
                    this.gridControl退回和退签订单.DataSource = MerchantService.getDeliveryOrder4(this.brandIdReturn);
                    this.gridView退回和退签订单.RefreshData();
                    gridView退回和退签订单.OptionsBehavior.AutoExpandAllGroups = true;
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear退回和退签品牌商Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit退回和退签品牌商.ToolTip = null;
            searchLookUpEdit退回和退签品牌商.EditValue = null;
        }
        private void searchLookUpEdit退回和退签品牌商_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.brandNameReturn;
        }
        #endregion

        private void simpleButton待揽收订单导出excel_Click(object sender, EventArgs e)
        {
            if (this.gridView待揽收订单.DataRowCount <= 0)
            {
                return;
            }
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.gridView待揽收订单.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void simpleButton在途数据导出Excel_Click(object sender, EventArgs e)
        {
            if (this.gridView在途订单.DataRowCount <= 0)
            {
                return;
            }
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.gridView在途订单.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void simpleButton签收数据导出Excel_Click(object sender, EventArgs e)
        {
            if (this.gridView签收订单.DataRowCount <= 0)
            {
                return;
            }
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.gridView签收订单.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void simpleButton退签数据导出Excel_Click(object sender, EventArgs e)
        {
            if (this.gridView退回和退签订单.DataRowCount <= 0)
            {
                return;
            }
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.gridView退回和退签订单.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}